var _columns = [{
        checkbox: true
    },
    {
        field: 'id',
        title: '标示符'
    },
    {
        field: 'showId',
        title: '序号',
        formatter: function (value, row, index) {
            return index + 1;
        }
    },
    {
        field: 'name',
        title: '规则名称'
    },
    //	{field: 'date', title: '创建时间',
    //	    formatter:function(value,row,index){
    //            var e = (row.date).substr(0,10);
    //            return e;
    //	}},
    {
        field: 'created_at',
        title: '创建时间',
        formatter: function (value, row, index) {
            return row.createDate;
        }
    },
    {
        field: 'defination',
        title: '备注'
    },
    {
        field: 'sensorSort',
        title: '设备类型'
    },
    {
        field: 'workshop',
        title: '所在位置'
    },
    {
        field: 'sensorRowId',
        title: '设备编号'
    },
    {
        field: 'ruleValue3',
        title: '三级预警范围',
        formatter: function (value, row, index) {
            return '[' + row.ruleValue3L + ',' + row.ruleValue2L + ')' + ',' + '[' + row.ruleValue2H + ',' + row.ruleValue3H + ')'
        }
    },
    {
        field: 'ruleValue2',
        title: '二级预警范围',
        formatter: function (value, row, index) {
            return '[' + row.ruleValue2L + ',' + row.ruleValue1L + ')' + ',' + '[' + row.ruleValue1H + ',' + row.ruleValue2H + ')'
        }
    },
    {
        field: 'ruleValue2',
        title: '一级预警范围',
        formatter: function (value, row, index) {
            return '[' + row.ruleValue1L + ',' + row.ruleValue0L + ')' + ',' + '[' + row.ruleValue0H + ',' + row.ruleValue1H + ')'
        }
    }
]

$('.selectpicker').selectpicker('val', 'mustard');
$('.selectpicker').selectpicker('val', ['mustard', 'relish']);
$('.selectpicker').selectpicker('refresh');

var TableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        $('#data_table').bootstrapTable({
            url: '/warning_rule/select_by_company_id', //请求后台的URL（*）
            method: 'get', //请求方式（*）
            toolbar: '#toolbar', //工具按钮用哪个容器
            cache: false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true, //是否显示分页（*）
            queryParams: oTableInit.queryParams, //传递参数（*）
            sidePagination: "client", //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1, //初始化加载第一页，默认第一页
            pageSize: 10, //每页的记录行数（*）
            pageList: [10, 25, 50, 100], //可供选择的每页的行数（*）
            showColumns: true, //是否显示所有的列
            showRefresh: true, //是否显示刷新按钮
            clickToSelect: true, //是否启用点击选中行
            uniqueId: "id", //每一行的唯一标识，一般为主键列
            columns: _columns,
        });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit, //页面大小
            offset: params.offset, //页码
            company_id: label_id, //公司编号
        };
        return temp;
    };
    return oTableInit;
};

var ButtonInit = function () {
    var oInit = new Object();
    oInit.Init = function () {
        $("#btn_page").click(function () {
            $.ajax({
                type: 'get',
                url: '/require/select',
                data: {
                    'id': 1,
                    'content': 'select',
                    'labelId': '1',
                },
                async: false,
                success: function (data) {

                }
            });

        })
        //初始化页面上面的按钮事件
        //新增按钮
        $("#btn_return").click(function () {
            $('#page-wrapper').load('pages/technological_process.html');
        })

        $("#btn_add").click(function () {
            //先选择所在位置
            $.ajax({
                type: 'get',
                url: '/chart/queryLocationByBusinessId',
                data: {
                    'businessId': label_id
                },
                dataType: 'json',
                async: false,
                contentType: "application/json; charset=utf-8",
                success: function (res) {
                    let data = res.data
                    $('#workshop').empty();
                    for (var i = 0; i < data.length; i++) {
                        $("#workshop").append(
                            "<option value='" + data[i] + "'>" + data[i] +
                            "</option>");
                    }
                },
            });
            $('#workshop').selectpicker("refresh");
            $('#newname').val("");
            $('#newdefination').val("");
            $('#newsensorRowId').val("");
            $('#newsensorRowId').empty();
            $('#newruleValue3h').val("");
            $('#newruleValue2h').val("");
            $('#newruleValue1h').val("");
            $('#newruleValue0h').val("");
            $('#newruleValue0l').val("");
            $('#newruleValue1l').val("");
            $('#newruleValue2l').val("");
            $('#newruleValue3l').val("");


            $('#sort').change(function () {
                //清除之前缓存数据
                $('#newsensorRowId').empty();
                var workshop = $("#workshop").val();
                workshop = "workshop=" + workshop;
                var sort = $("#sort").val();
                sort = "sort=" + sort;
                let businessId = "businessId=" + label_id

                //根据车间和类型查传感器编号
                $.ajax({
                    type: 'get',
                    url: '/warning_record/querySensorRowIdByWorkShopAndSort?'+ businessId + "&" + workshop + '&' + sort,
                    async: false,
                    success: function (data) {
                        for (var i = 0; i < data.length; i++) {
                            $("#newsensorRowId").append(
                                "<option value='" + data[i] + "'>" +
                                data[i] + "</option>");
                        }
                        $("#newsensorRowId").selectpicker('refresh')
                    }
                });
            });

            $('#workshop').change(function () {
                //清除之前缓存数据
                $('#sort').empty();
                $('#newsensorRowId').empty();
                //先查询设备类型
                $.ajax({
                    type: 'get',
                    url: '/warning_rule/querySensorTypeByLocation',
                    data: {
                        'businessId': label_id,
                        'locations': JSON.stringify($("#workshop").val()),
                    },
                    dataType: 'json',
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    success: function (res) {
                        let data = res.list
                        $('#sort').empty();
                        for (var i = 0; i < data.length; i++) {
                            $("#sort").append(
                                "<option value='" + data[i] + "'>" + data[i] +
                                "</option>");
                        }
                    },
                });
            });
            $('#new_data').modal();
        });

        //编辑按钮
        $("#btn_edit").click(function () {
            var arrselections = $("#data_table").bootstrapTable('getSelections');
            if (arrselections.length > 1) {
                bootoast({
                    message: '只能选择一行进行编辑',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (arrselections.length <= 0) {
                bootoast({
                    message: '请选择有效数据',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            $('#editname').val(arrselections[0].name);
            $('#editWorkshop').val(arrselections[0].workshop);
            $('#editSensorRowId').val(arrselections[0].sensorRowId);
            $('#editdefination').val(arrselections[0].defination);
            $('#editruleValue3H').val(arrselections[0].ruleValue3H);
            $('#editruleValue2H').val(arrselections[0].ruleValue2H);
            $('#editruleValue1H').val(arrselections[0].ruleValue1H);
            $('#editruleValue0H').val(arrselections[0].ruleValue0H);
            $('#editruleValue0L').val(arrselections[0].ruleValue0L);
            $('#editruleValue1L').val(arrselections[0].ruleValue1L);
            $('#editruleValue2L').val(arrselections[0].ruleValue2L);
            $('#editruleValue3L').val(arrselections[0].ruleValue3L);
            $('#edit_data').modal()
        });

        // 删除按钮
        $("#btn_delete").click(function () {
            var arrselections = $("#data_table").bootstrapTable('getSelections');
            if (arrselections.length <= 0) {
                bootoast({
                    message: '请选择有效数据',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            values = [];
            for (var i = 0; i < arrselections.length; i++)
                values.push(arrselections[i].id);
            bootbox.confirm({
                message: "确认要删除选择的数据吗？",
                locale: 'zh_CN',
                title: '操作提示',
                callback: function (result) {
                    if (result) {
                        $.ajax({
                            type: "post",
                            url: "/warning_rule/delete",
                            data: JSON.stringify(values),
                            contentType: "application/json",
                            success: function (data) {
                                if (data['status'] == "success") {
                                    bootoast({
                                        message: '删除成功',
                                        position: 'top',
                                        type: 'success',
                                        timeout: 1.5,
                                    });
                                    $("#data_table").bootstrapTable('refresh');
                                } else {
                                    bootoast({
                                        message: '删除失败\n' + data['msg'],
                                        position: 'top',
                                        type: 'danger',
                                        timeout: 1.5,
                                    });
                                }
                            }
                        });
                    }
                }
            })

        });


        //新增框内的提交
        $('#new_data_submit').on('click', function () {

            var values = "";
            //for (var i = 3; i < _columns.length; i++)
            // for (var i = 3; i < 6; i++)
            //values+=_columns[i].field+"="+$('#new' + _columns[i].field).val()+"&";
            var name = $('#newname').val();
            values += "name=" + name + "&";
            var sensorRowId = $('#newsensorRowId').val();
            var defination = $('#newdefination').val();
            values += "defination=" + defination + "&";
            values += "sensorRowId=" + sensorRowId + "&";
            //var sensorType = $('#newsensorType').val();
            //var ruleType = $('#newruleType').val();
            var ruleValue3h = $('#newruleValue3h').val();
            values += "ruleValue3H=" + ruleValue3h + "&";
            var ruleValue2h = $('#newruleValue2h').val();
            values += "ruleValue2H=" + ruleValue2h + "&";
            var ruleValue1h = $('#newruleValue1h').val();
            values += "ruleValue1H=" + ruleValue1h + "&";
            var ruleValue0h = $('#newruleValue0h').val();
            values += "ruleValue0H=" + ruleValue0h + "&";
            var ruleValue0l = $('#newruleValue0l').val();
            values += "ruleValue0L=" + ruleValue0l + "&";
            var ruleValue1l = $('#newruleValue1l').val();
            values += "ruleValue1L=" + ruleValue1l + "&";
            var ruleValue2l = $('#newruleValue2l').val();
            values += "ruleValue2L=" + ruleValue2l + "&";
            var ruleValue3l = $('#newruleValue3l').val();
            values += "ruleValue3L=" + ruleValue3l + "&";
            values += "companyId=" + label_id;

            if (name == "") {
                bootoast({
                    message: '请输入规则名称',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (sensorRowId == "") {
                bootoast({
                    message: '请输入设备编号',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue3h == "") {
                bootoast({
                    message: '请输入三级预警高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }

            if (ruleValue2h == "") {
                bootoast({
                    message: '请输入二级预警高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue1h == "") {
                bootoast({
                    message: '请输入一级预警高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue0h == "") {
                bootoast({
                    message: '请输入正常运行高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue0l == "") {
                bootoast({
                    message: '请输入正常运行低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue1l == "") {
                bootoast({
                    message: '请输入一级预警低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue2l == "") {
                bootoast({
                    message: '请输入二级预警低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue3l == "") {
                bootoast({
                    message: '请输入三级预警低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (isNaN(Number(ruleValue3h))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '三级预警高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue2h))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '二级预警高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue1h))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '一级预警高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue0h))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '正常运行高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue0l))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '正常运行低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue1l))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '一级预警低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue2l))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '二级预警低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue3l))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '三级预警低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            $.ajax({
                type: "get",
                url: "/warning_rule/insert?" + values,
                success: function (data) {
                    if (data['status'] == "success") {
                        bootoast({
                            message: '插入成功',
                            position: 'top',
                            type: 'success',
                            timeout: 2,
                        });
                        $("#new_data").modal('hide');
                        $('#newname').val("");
                        $('#newsensorRowId').val("");
                        $('#newdefination').val("");
                        // $('#newsensorType').val("");
                        // $('#newruleType').val("");
                        $('#newruleValue3h').val("");
                        $('#newruleValue2h').val("");
                        $('#newruleValue1h').val("");
                        $('#newruleValue0h').val("");
                        $('#newruleValue0l').val("");
                        $('#newruleValue1l').val("");
                        $('#newruleValue2l').val("");
                        $('#newruleValue3l').val("");

                        $("#data_table").bootstrapTable('refresh');

                    } else {
                        bootoast({
                            message: '插入失败\n' + data['msg'],
                            position: 'top',
                            type: 'danger',
                            timeout: 2,
                        });
                    }
                },
                error: function () {
                    bootoast({
                        message: '通讯异常',
                        position: 'top',
                        type: 'danger',
                        timeout: 1.5,
                    });
                }
            });
        });

        //编辑框内的提交
        $('#edit_data_submit').click(function () {
            var id = $("#data_table").bootstrapTable('getSelections')[0]['id'];
            var values = "id=" + id + "&";
            //var name = $('#editname').val();
            //var sensorRowId = $('#editsensorRowId').val();
            var defination = $('#editdefination').val();
            values = values + "defination=" + defination + "&";
            // var sensorType = $('#editsensorType').val();
            var ruleValue3H = $('#editruleValue3H').val();
            values = values + "ruleValue3H=" + ruleValue3H + '&';
            var ruleValue2H = $('#editruleValue2H').val();
            var ruleValue1H = $('#editruleValue1H').val();
            var ruleValue0H = $('#editruleValue0H').val();
            var ruleValue0L = $('#editruleValue0L').val();
            var ruleValue1L = $('#editruleValue1L').val();
            var ruleValue2L = $('#editruleValue2L').val();
            var ruleValue3L = $('#editruleValue3L').val();
            values = values + "ruleValue2H=" + ruleValue2H + '&';
            values = values + "ruleValue1H=" + ruleValue1H + '&';
            values = values + "ruleValue0H=" + ruleValue0H + '&';
            values = values + "ruleValue0L=" + ruleValue0L + '&';
            values = values + "ruleValue1L=" + ruleValue1L + '&';
            values = values + "ruleValue2L=" + ruleValue2L + '&';
            values = values + "ruleValue3L=" + ruleValue3L;
            if (ruleValue3H == "") {
                bootoast({
                    message: '请输入三级预警高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }

            if (ruleValue2H == "") {
                bootoast({
                    message: '请输入二级预警高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue1H == "") {
                bootoast({
                    message: '请输入一级预警高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue0H == "") {
                bootoast({
                    message: '请输入正常运行高限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue0L == "") {
                bootoast({
                    message: '请输入正常运行低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue1L == "") {
                bootoast({
                    message: '请输入一级预警低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue2L == "") {
                bootoast({
                    message: '请输入二级预警低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (ruleValue3L == "") {
                bootoast({
                    message: '请输入三级预警低限值',
                    position: 'top',
                    type: 'warning',
                    timeout: 1.5,
                });
                return;
            }
            if (isNaN(Number(ruleValue3H))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '三级预警高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue2H))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '二级预警高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue1H))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '一级预警高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue0H))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '正常运行高限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue0L))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '正常运行低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue1L))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '一级预警低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue2L))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '二级预警低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            if (isNaN(Number(ruleValue3L))) //数值不等于NaN,Number转化为数字类型，isNaN是否为NaN
            {
                bootoast({
                    message: '三级预警低限值请输入数字类型',
                    position: 'top',
                    type: 'danger',
                    timeout: 2,
                });
                return;
            }
            $.ajax({
                type: "get",
                url: "/warning_rule/update?" + values,
                success: function (data) {
                    if (data['status'] == "success") {
                        bootoast({
                            message: '修改成功',
                            position: 'top',
                            type: 'success',
                            timeout: 2,
                        });
                        $("#edit_data").modal('hide');
                        $("#data_table").bootstrapTable('refresh');
                    } else {
                        bootoast({
                            message: '修改失败\n' + data['msg'],
                            position: 'top',
                            type: 'danger',
                            timeout: 2,
                        });
                    }
                },
                error: function () {
                    bootoast({
                        message: '通讯异常',
                        position: 'top',
                        type: 'danger',
                        timeout: 1.5,
                    });
                }
            });
        });
    };

    return oInit;
};

$(function () {
    //1.初始化Table
    var oTable = new TableInit();
    oTable.Init();

    //2.初始化Button的点击事件
    var oButtonInit = new ButtonInit();
    oButtonInit.Init();

    //隐藏数据库表id列
    $('#data_table').bootstrapTable('hideColumn', 'id');

    queryPermission('预警/预警规则/预警规则', '新增', 'btn_add')
    queryPermission('预警/预警规则/预警规则', '编辑', 'btn_edit')
    queryPermission('预警/预警规则/预警规则', '删除', 'btn_delete')
});

function getSelectNo() {
    $.ajax({
        type: 'get',
        url: '/chart/querySensorTypeByLocation',
        data: {
            'businessId': label_id,
            'location': $("#work_id").val()
        },
        dataType: 'json',
        async: false,
        contentType: "application/json; charset=utf-8",
        success: function (res) {

            let data = res.data

            $('#select_no').empty();
            for (var i = 0; i < data.length; i++) {
                $("#select_no").append(
                    "<option value='" + data[i] + "'>" + data[i] +
                    "</option>");
            }
        },
    });
}

/**
 * 获取设备编号
 */
function getSelectType() {
    $.ajax({
        type: 'get',
        url: '/chart/querySensorIdByLocationAndType',
        data: {
            'businessId': label_id,
            'location': $("#work_id").val(),
            'sensorType': $("#select_no").val(),
        },
        dataType: 'json',
        async: false,
        contentType: "application/json; charset=utf-8",
        success: function (res) {

            let data = res.data
            $('#select_type').empty();
            for (var i = 0; i < data.length; i++) {
                $("#select_type").append(
                    "<option value='" + data[i] + "'>" + data[i] +
                    "</option>");
            }
            $("#select_type").trigger("change")
        },
    });
}